﻿using System;
using System.Configuration;
using System.Timers;
using log4net;

namespace CollectHongKongCompanies
{
    class Program
    {
        static void Main(string[] args)
        {
            //new InitializationDB().InitializationSeeder();

            int waitSpan = Convert.ToInt32(ConfigurationManager.AppSettings["ExecSpan"]) * 60 * 1000;

            var execTimer = new System.Timers.Timer(1000);

            execTimer.Elapsed += new ElapsedEventHandler(ExecCollect);

            // Set the Interval to 2 seconds (2000 milliseconds).
            execTimer.Interval = waitSpan;
            execTimer.Enabled = true;

            Console.ReadLine();
        }

        static void ExecCollect(object source, ElapsedEventArgs e)
        {
            var startTime = DateTime.Now;

            ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            logger.Info(string.Format("{0}开始处理", startTime));

            try
            {
                new RequestController().SendRequest();
                var finishTime = DateTime.Now;
                logger.Info(string.Format("{0}处理结束", finishTime));

                var span = finishTime - startTime;
                logger.Info(string.Format("处理共耗时 {0} 分钟", span.TotalMinutes));
            }
            catch (Exception ex)
            {
                logger.Error("执行出错啦！", ex);
            }
            Console.WriteLine("開始休息{0}分鐘", ConfigurationManager.AppSettings["ExecSpan"]);
            logger.Info("-----------------------------------------------------");
        }
    }
}
